/*
 *  Beam.cpp
 *  
 *	Version 1.0
 *  Created by Nicholas Blecha on 9/25/10.
 *  Copyright 2010 Michigan Technological University. All rights reserved.
 *
 */

#include "Beam.h"

int Beam::getDamage() {
	return damage;
}

int Beam::getSpeed() {
	return speed;
}

void Beam::autoMove(double ticks) {
	position.x -= (int) ((ticks/1000.f)*(30*(speed * cos(angle+M_PI/2))));		//Again, needing speed adjustments
	position.y -= (int) ((ticks/1000.f)*(30*(speed * sin(angle+M_PI/2))));		//Using temporary x24 boost
}

void Beam::show() {
	if (rnd!=NULL)
		rnd->drawSurface(&position, radiansToDegrees(angle), texId);
}

void Beam::show(int mapX, int mapY){
	drawPos->x=position.x-mapX;
	drawPos->y=position.y-mapY;
	rnd->drawSurface(drawPos, radiansToDegrees(angle), texId);
	hitbox->calcWorldCoords(drawPos->x, drawPos->y, angle);
	rnd->drawBox(hitbox->getWorldCoord(0), hitbox->getWorldCoord(1), hitbox->getWorldCoord(2), hitbox->getWorldCoord(3));
	hitbox->clearWorldCoords();
}

void Beam::setActive(bool a) {
	active = a;
}

void Beam::initialize(int dmg, int spd, float a, int x, int y) {
	damage = dmg;
	speed = spd;
	angle = a;
	position.x = x;
	position.y = y;
}


void Beam::setImage(int color) {
	if (color<0 || color>=NUM_OF_COLORS) {
		return;
	}
	image = lasers[color];
	texId = ids[color];
}



Beam::Beam(Renderer *rend, int i) : Sprite() {
	if (rend==NULL) {
		throw "NULL renderer";
	}
	rnd = rend;
	id = i;	
	damage = 0;
	speed = 0;
	angle = 0;
	lasers[GREEN] = rnd->loadImage("Images//laser0.png");
	ids[GREEN] = rnd->createTexture(lasers[GREEN]);
	lasers[RED] = rnd->loadImage("Images//laser1.png");
	ids[RED] = rnd->createTexture(lasers[RED]);
	lasers[BLUE] = rnd->loadImage("Images//laser2.png");
	ids[BLUE] = rnd->createTexture(lasers[BLUE]);
	lasers[MISSLE] = rnd->loadImage("Images//Missle.png");
	ids[MISSLE] = rnd->createTexture(lasers[MISSLE]);
	setImage(GREEN);
	active = false;

	drawPos=new SDL_Rect();
	drawPos->w = position.w = image->w;
	drawPos->h = position.h=  image->h;
	//hitbox = new Hitbox(17, 16, 5, 16);
	hitbox = new Hitbox(17, 16, 5, 16);
	hitbox->setIdentifier("Beam");
	radius = 16;
}

Beam::~Beam() {
	delete hitbox;
	for (int i=0; i<3; i++) {
		SDL_FreeSurface(lasers[i]);
	}
	delete drawPos;
}
